﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;

namespace YXShop.Web.membercenter.individualinfo
{
    public partial class memberinfo_edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                InitWebControl();
                if (Session["MemberID"]!=null)
                {
                    string memberId = Session["MemberID"].ToString();
                    ViewState["memberId"] = memberId;
                    GetInfo(Convert.ToInt32(memberId));
                    GetAccount(Convert.ToInt32(memberId));
                }      
            }

        }

        #region 初试化
        private void InitWebControl()
        {
       
            ChangeHope.WebPage.WebControl.Validate(this.txtEmail, "会员的邮件，用于发送一些与用户相关的信息，比如密码发送等。", "isemail", "邮件", "该项必须为邮件");
            this.Form.Attributes.Add("onsubmit", "return CheckForm();");
            //会员信息
            ChangeHope.WebPage.WebControl.SetDate(this.txtBirthday);
            ChangeHope.WebPage.WebControl.SetDropDownList(this.ddlSex, "code", "content", "yxs_code_sex");
            ChangeHope.WebPage.WebControl.SetDropDownList(this.ddlMarriage, "code", "content", "yxs_code_marriage");
            ChangeHope.WebPage.WebControl.SetDropDownList(this.ddlPapersType, "code", "content", "yxs_code_papers");
            ChangeHope.WebPage.WebControl.SetDropDownList(this.ddlEducation, "code", "content", "yxs_code_education");

            //联系方式
            this.ddlProvince.Attributes.Add("onchange", "GetCity('" + this.ddlCity.ClientID + "',this.value);");
            this.ddlCity.Attributes.Add("onchange", "GetCity('" + this.ddlBorough.ClientID + "',this.value);");
            this.lblScript.Text = "GetCity('" + this.ddlProvince.ClientID + "','0');\n";
        }
        #endregion

        #region 绑定个人详细信息
        private void GetInfo(int uid)
        {
            YXShop.BLL.Member.MemberInfo bll = new YXShop.BLL.Member.MemberInfo();
            YXShop.Model.Member.MemberInfo model = bll.GetModel(uid);
            if (model != null)
            {
                this.txtTrueName.Text = model.TrueName;
                this.txtTitle.Text = model.Title;
                this.txtBirthday.Text = model.Birthday.ToString();
                this.ddlPapersType.SelectedValue = model.PapersType;
                this.txtPapersNumber.Text = model.PapersNumber;
                this.txtOrigin.Text = model.Origin;
                this.txtNation.Text = model.Nation;
                this.ddlSex.SelectedValue = model.Sex.ToString();
                this.ddlMarriage.SelectedValue = model.Marriage.ToString();
                this.ddlEducation.SelectedValue = model.Education;
                this.txtGraduateSchool.Text = model.GraduateSchool;
                //this.txtProvince.Text = model.Province;
                this.lblScript.Text += "GetCity('" + this.ddlProvince.ClientID + "','0','" + model.Province + "');\n";
                this.lblScript.Text += "GetCity('" + this.ddlCity.ClientID + "','" + model.Province + "','" + model.City + "');\n";
                this.lblScript.Text += "GetCity('" + this.ddlBorough.ClientID + "','" + model.City + "','" + model.Borough + "');\n";
                //this.txtCity.Text = model.City;
                //this.txtBorough.Text = model.Borough;
                this.txtAddress.Text = model.Address;
                this.txtZip.Text = model.Zip;
                this.txtOfficePhone.Text = model.OfficePhone;
                this.txtHomePhone.Text = model.HomePhone;
                this.txtMobilePhone.Text = model.MobilePhone;
                this.txtHandPhone.Text = model.HandPhone;
                this.txtFax.Text = model.Fax;
                this.txtPersonWebSite.Text = model.PersonWebSite;
                this.txtQQ.Text = model.QQ;
                this.txtMSN.Text = model.MSN;
                this.txtICQ.Text = model.ICQ;
                this.txtUC.Text = model.UC;
                this.txtLifeHobbies.Text = model.LifeHobbies;
                this.txtCultureHobbies.Text = model.CultureHobbies;
                this.txtEntertainment.Text = model.Entertainment;
                this.txtSportsHobbies.Text = model.SportsHobbies;
                this.txtOtherHobbies.Text = model.OtherHobbies;
                this.txtIncName.Text = model.IncName;
                this.txtDepartment.Text = model.Department;
                this.txtPositions.Text = model.Positions;
                this.txtWorkRange.Text = model.WorkRange;
                this.txtIncAddress.Text = model.IncAddress;
                this.txtMonthlyInCome.Text = model.MonthlyInCome;

            }
        }
        #endregion

        #region 绑定基本信息
        private void GetAccount(int id)
        {
            YXShop.BLL.Member.MemberAccount bll = new YXShop.BLL.Member.MemberAccount();
            YXShop.Model.Member.MemberAccount model = bll.GetModel(id);
            if (model != null)
            {                
                this.lblUserId.Text = model.UserId;
                this.txtSigned.Text = model.Signed;
                this.lblQuestion.Text = model.Question;
                this.txtEmail.Text = model.Email;
           
                ViewState["RegisterDate"] = model.RegisterDate.ToString();
                ViewState["RegisterIP"] = model.RegisterIP.ToString();
                ViewState["LastLoginDate"] = model.LastLoginDate.ToString();
                ViewState["LastLoginIP"] = model.LastLoginIP.ToString();
                ViewState["LoginTimes"] = model.LoginTimes.ToString();
                ViewState["Capital"] = model.Capital.ToString();
                ViewState["Coupons"] = model.Coupons.ToString();
                ViewState["Points"] = model.Points.ToString();
                ViewState["PeriodOfValidity"] = model.PeriodOfValidity.ToString();
            }
            model = null;
            bll = null;
        }
        #endregion
        //保存修改
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {             
              UpdateInfo();                       
            }
            catch (Exception)
            {
                this.ltlMsg.Text = "编辑个人资料失败";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }         
        }

        private void UpdateInfo()
        {
            YXShop.BLL.Member.MemberInfo bll = new YXShop.BLL.Member.MemberInfo();
            if (ViewState["memberId"] != null)
            {
                 Model.Member.MemberInfo model=bll.GetModel(Convert.ToInt32(ViewState["memberId"]));
                 model.TrueName = txtTrueName.Text;
                 model.Title = txtTitle.Text;
                 model.Photo = "";//txtPhoto.Text;
                 model.Birthday = ChangeHope.Common.StringHelper.StringToDateTime(txtBirthday.Text);
                 model.PapersType = ddlPapersType.SelectedValue;
                 model.PapersNumber = txtPapersNumber.Text;
                 model.Origin = txtOrigin.Text;
                 model.Nation = txtNation.Text;
                 model.Sex = ChangeHope.Common.StringHelper.StringToInt(ddlSex.SelectedValue);
                 model.Marriage = ChangeHope.Common.StringHelper.StringToInt(ddlMarriage.SelectedValue);
                 model.Education = ddlEducation.SelectedValue;
                 model.GraduateSchool = txtGraduateSchool.Text;
                 model.Province = ChangeHope.WebPage.PageRequest.GetFormString("ctl00$workspace$ddlProvince");
                 model.City = ChangeHope.WebPage.PageRequest.GetFormString("ctl00$workspace$ddlCity");
                 model.Borough = ChangeHope.WebPage.PageRequest.GetFormString("ctl00$workspace$ddlBorough");
                 model.Address = txtAddress.Text;
                 if(txtZip.Text!="")
                 {
                     if (!ChangeHope.Common.ValidateHelper.IsSend(txtZip.Text.Trim()))
                     {
                         this.ltlMsg.Text = "请输入正确的邮政编码";
                         this.pnlMsg.Visible = true;
                         this.pnlMsg.CssClass = "actionErr";
                         return;
                     }
                 }
                 if (txtOfficePhone.Text!="")
                 {
                     if (!ChangeHope.Common.ValidateHelper.IsPhone(txtOfficePhone.Text))
                     {
                         this.ltlMsg.Text = "请输入正确的电话号码";
                         this.pnlMsg.Visible = true;
                         this.pnlMsg.CssClass = "actionErr";
                         return;
                     }
                 }
                 if (txtMobilePhone.Text!="")
                 {
                     if (!ChangeHope.Common.ValidateHelper.IsMobilePhone(txtMobilePhone.Text))
                     {
                         this.ltlMsg.Text = "请输入正确的手机号码";
                         this.pnlMsg.Visible = true;
                         this.pnlMsg.CssClass = "actionErr";
                         return;
                     }
                 }
                if (txtHomePhone.Text!="")
                {
                    if (!ChangeHope.Common.ValidateHelper.IsPhone(txtHomePhone.Text))
                     {
                         this.ltlMsg.Text = "请输入正确的住宅电话号码";
                         this.pnlMsg.Visible = true;
                         this.pnlMsg.CssClass = "actionErr";
                         return;
                     }
                }
                if (txtHandPhone.Text!="")
                {
                    if (!ChangeHope.Common.ValidateHelper.IsPhone(txtHandPhone.Text))
                    {
                        this.ltlMsg.Text = "请输入正确的小灵通号码";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                        return;
                    }
                }
                if (txtFax.Text!="")
                {
                    if (!ChangeHope.Common.ValidateHelper.IsPhone(txtFax.Text))
                    {
                        this.ltlMsg.Text = "请输入正确的传真号码";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                        return;
                    }
                }
                if (txtPersonWebSite.Text!="")
                {
                    if (!ChangeHope.Common.ValidateHelper.IsUrl(txtPersonWebSite.Text))
                    {
                        this.ltlMsg.Text = "请输入正确的网络地址";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                        return;
                    }
                }
                if (txtQQ.Text!="")
                {
                    if (!ChangeHope.Common.ValidateHelper.IsNumber(txtQQ.Text) || txtQQ.Text.Length < 5 || txtQQ.Text.Length > 13)
                    {
                        this.ltlMsg.Text = "请输入正确的QQ号码";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                        return;
                    }
                }
                if (txtUC.Text!="")
                {
                    if (!ChangeHope.Common.ValidateHelper.IsNumber(txtUC.Text) || txtUC.Text.Length < 5 || txtUC.Text.Length > 13)
                    {
                        this.ltlMsg.Text = "请输入正确的UC号码";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                        return;
                    }
                }
                 model.Zip = txtZip.Text;
                 model.OfficePhone = txtOfficePhone.Text;
                 model.HomePhone = txtHomePhone.Text;
                 model.MobilePhone = txtMobilePhone.Text;
                 model.HandPhone = txtHandPhone.Text;
                 model.Fax = txtFax.Text;
                 model.PersonWebSite = txtPersonWebSite.Text;
                 model.QQ = txtQQ.Text;
                 model.MSN = txtMSN.Text;
                 model.ICQ = txtICQ.Text;
                 model.UC = txtUC.Text;
                 model.LifeHobbies = txtLifeHobbies.Text;
                 model.CultureHobbies = txtCultureHobbies.Text;
                 model.Entertainment = txtEntertainment.Text;
                 model.SportsHobbies = txtSportsHobbies.Text;
                 model.OtherHobbies = txtOtherHobbies.Text;
                 model.IncName = txtIncName.Text;
                 model.Department = txtDepartment.Text;
                 model.Positions = txtPositions.Text;
                 model.WorkRange = txtWorkRange.Text;
                 model.IncAddress = txtIncAddress.Text;
                 if (txtMonthlyInCome.Text.Trim() != "")
                 {
                     if (!Isnum(txtMonthlyInCome.Text.Trim()))
                     {
                         this.ltlMsg.Text = "编辑个人月收入必须是数字";
                         this.pnlMsg.Visible = true;
                         this.pnlMsg.CssClass = "actionErr";
                         return;
                     }
                 }
                 model.MonthlyInCome = txtMonthlyInCome.Text;
                 bll.Update(model);
                 this.ltlMsg.Text = "编辑个人资料成功";
                 this.pnlMsg.Visible = true;
                 this.pnlMsg.CssClass = "actionOk";
            }
 
        }

        #region 判断
        public bool Isnum(string values)
        {
            if (values != null && values != "")
            {
                string pattern = @"^(0?|[1-9]\d*)(\.\d{0,2})?$";
                return Regex.IsMatch(values, pattern);
            }
            else
            {
                return false;
            }
        }
        #endregion

        private bool UpdateAccount()
        {
            YXShop.BLL.Member.MemberAccount accountBll = new YXShop.BLL.Member.MemberAccount();
            if (ViewState["memberId"]!=null)
            {
                YXShop.Model.Member.MemberAccount model = accountBll.GetModel(Convert.ToInt32(ViewState["memberId"]));
                model.PassWord = "";
                model.RegisterDate = Convert.ToDateTime(ViewState["RegisterDate"].ToString());
                model.RegisterIP = ViewState["RegisterIP"].ToString();
                model.LastLoginDate = Convert.ToDateTime(ViewState["LastLoginDate"].ToString());
                model.LastLoginIP = ViewState["LastLoginIP"].ToString();
                model.LoginTimes = Convert.ToInt32(ViewState["LoginTimes"].ToString());
                model.Capital = Convert.ToDecimal(ViewState["Capital"].ToString());
                model.Coupons = Convert.ToDecimal(ViewState["Coupons"].ToString());
                model.Points = Convert.ToDecimal(ViewState["Points"].ToString());
                model.PeriodOfValidity = Convert.ToDateTime(ViewState["PeriodOfValidity"].ToString());
                model.Email=this.txtEmail.Text.Trim().ToString();
                string type = this.radType.SelectedValue;
                if(type=="1")
                {
                    string oldAnswer = this.txtOldAnswer.Text.Trim().ToString();
                    if (oldAnswer == "")
                    {
                        this.palOld.CssClass = "actionErr";
                        this.pnlMsg.Visible = false;
                        return false;
                    }
                    else
                    {
                        if (oldAnswer.Equals(model.Answer))
                        {
                            if (this.txtNewAnswer.Text.Trim().ToString() != "")
                            {   
                                model.Answer = this.txtNewAnswer.Text.Trim().ToString();
                                this.palNew.CssClass = "msgNormal";
                            }
                            else
                            {
                                this.palNew.CssClass = "actionErr";
                                this.pnlMsg.Visible = false;
                                return false;
                            }
                        }
                        else
                        {
                            this.ltlMsg.Text = "操作失败：原问题答案与你注册时不一直";
                            this.pnlMsg.Visible = true;
                            this.pnlMsg.CssClass = "actionErr";
                            return false;
                            
                        }
                        this.palOld.CssClass = "msgNormal";
                    }
                    
                }
                accountBll.Update(model);
                return true;
            }
            return false;
        }
    }
}
